//
//  MineVC.m
//  K8彩票
//
//  Created by zyn on 2017/6/6.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "MineVC.h"

#import <Masonry.h>
#import "ContactViewController.h"
#import "AboutViewController.h"
#import "LuckyLotteryViewController.h"
#import <MessageUI/MessageUI.h>
#import "DutyViewController.h"
#import "MineHeadview.h"
#import "Head.h"
#import "MineTableViewCell.h"

@interface MineVC ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic,strong) UITableView* tableview;

@property (nonatomic,strong) NSArray* models;
@end

@implementation MineVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initializeView];
//    
    self.tableview.tableHeaderView = [[NSBundle mainBundle]loadNibNamed:@"MineHead" owner:self options:nil].lastObject;


}




-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [UIView animateWithDuration:1 animations:^{
        ViewController* con = [UIApplication sharedApplication].keyWindow.rootViewController;
        con.mainTabbar.alpha = 1;
    }];
}

//去掉UItableview headerview黏性 设置tableivew与headerview的间距
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableview)
    {
        CGFloat sectionHeaderHeight = 12;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}


-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    
    
    
}

- (void)initializeView{
    self.navigationController.navigationBarHidden = YES;
    
    UITableView* tableview = [[UITableView alloc]init];

    self.tableview = tableview;
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.view addSubview:self.tableview];
    
    self.tableview.frame = CGRectMake(0, 0, SWidth, SHeight);
    [tableview registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
//    UIView* splidview = [[UIView alloc]init];
//    splidview.frame = CGRectMake(0, cell.bounds.size.height-0.5, ScreenWidth(), 0.5);
//    splidview.backgroundColor = [UIColor grayColor];
//    [cell.contentView addSubview:splidview];
    
    cell.label.text = self.models[indexPath.row];
    cell.textLabel.textColor=[UIColor grayColor];

    
    if (indexPath.row != self.models.count-1) {
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.imgview.image = [UIImage imageNamed:@"accessory"];

    }
    else
    {
        UILabel* lbl = [[UILabel alloc]init];
        lbl.text = @"版本: 1.0.0";
        lbl.font = [UIFont systemFontOfSize:12];
        lbl.textColor = [UIColor grayColor];
        [lbl sizeToFit];
        [cell.contentView addSubview:lbl];

        [lbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell).offset(-5);
            make.centerY.equalTo(cell);
            
        }];
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    self.navigationController.navigationBarHidden = false;

    if (indexPath.row == 1) {
//        [UIView animateWithDuration:1 animations:^{
//            ViewController* con = [UIApplication sharedApplication].keyWindow.rootViewController;
//            con.mainTabbar.alpha = 0;
//        }];

        // 意见反馈页面
        [self contactusWithEmail];
    }
    else if (indexPath.row == 2)
    {
        [UIView animateWithDuration:1 animations:^{
            ViewController* con = [UIApplication sharedApplication].keyWindow.rootViewController;
            con.mainTabbar.alpha = 0;
        }];

        //关于我们界面
        [self.navigationController pushViewController:[AboutViewController new] animated:YES];
    }
    else if (indexPath.row == 3)
    {
        [UIView animateWithDuration:1 animations:^{
            ViewController* con = [UIApplication sharedApplication].keyWindow.rootViewController;
            con.mainTabbar.alpha = 0;
        }];

        //免责声明界面
        [self.navigationController pushViewController:[DutyViewController new] animated:YES];
    }
    else if (indexPath.row == 0)
    {
        [UIView animateWithDuration:1 animations:^{
            ViewController* con = [UIApplication sharedApplication].keyWindow.rootViewController;
            con.mainTabbar.alpha = 0;
        }];

        LuckyLotteryViewController* lucyCon = [[LuckyLotteryViewController alloc]init];

        [self.navigationController pushViewController:lucyCon animated:YES];

    }

}

-(NSArray *)models
{
    if (_models == nil) {
        _models = @[@"抽取幸运彩票",@"意见反馈",@"关于我们",@"免责声明",@"版本信息"];
    }
    return _models;
}

-(void)contactusWithEmail
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {
        // We must always check whether the current device is configured for sending emails
        if ([mailClass canSendMail])
        {
            [self sendFeedBackMail];
        }
        else
        {
            [self alertController];
        }
    }
    

}


- (void)alertController
{
    // 危险操作:弹框提醒
    // 1.UIAlertView
    // 2.UIActionSheet
    // iOS8开始:UIAlertController == UIAlertView + UIActionSheet
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"邮箱未设置" message:@"请先设置您的邮箱,或者自行联系2497992599@qq.com." preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加按钮
    __weak typeof(alert) weakAlert = alert;
    [alert addAction:[UIAlertAction actionWithTitle:@"了解" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        //        NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"取消", @"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        //        NSLog(@"点击了取消按钮");
    }]];
    
    //    [alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    //        NSLog(@"点击了其它按钮");
    //    }]];
    
    //    // 添加文本框
    //    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    //        textField.textColor = [UIColor redColor];
    //        textField.text = @"123";
    //        [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
    //        //        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    //    }];
    //    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    //        textField.secureTextEntry = YES;
    //        textField.text = @"123";
    //    }];
    
    [self presentViewController:alert animated:YES completion:nil];
    [[NSUserDefaults standardUserDefaults] setObject:@1 forKey:@"alert"];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)sendFeedBackMail{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"subjbect"];
    picker.navigationBar.tintColor = [UIColor redColor];
    NSArray *toRecipients = [NSArray arrayWithObject:@"1437955342@qq.com"];
    [picker setToRecipients:toRecipients];
    NSString *emailBody = [NSString
                           stringWithFormat:@"SystemVersion: %@ \n AppVersion: %@ \n\n 反馈信息:\n",                                           [[UIDevice currentDevice] systemVersion],
                           [[[NSBundle mainBundle] infoDictionary]
                            objectForKey:@"CFBundleShortVersionString"]];
    [picker setMessageBody:emailBody isHTML:NO];
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
    NSString *title = @"邮件发送提醒";
    NSString *msg;
    switch (result){
        case MFMailComposeResultCancelled:
            msg = @"邮件已被取消";
            break;
        case MFMailComposeResultSaved:
            msg = @"邮件保存成功";
            [self alertWithTitle:title msg:msg];
            break;
        case MFMailComposeResultSent:
            msg = @"邮件发送成功";
            [self alertWithTitle:title msg:msg];
            break;
        case MFMailComposeResultFailed:
            msg =@"邮件发送失败";
            [self alertWithTitle:title msg:msg];
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
                                                    message:msg
                                                   delegate:nil
                                          cancelButtonTitle:@"好"
                                          otherButtonTitles:nil];
    [alert show];
}


@end
